<?php

namespace app\common\model\order;

use think\Model;
use traits\model\SoftDelete;

class Aftersales extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order_aftersale';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'return_time_text',
        'aftersale_status_text',
        'refund_status_text',
        'confirm_time_text',
        'refund_time_text',
        'agree_time_text'
    ];

    public $checkinfoList = ['申请中','同意退款','拒绝退款'];
    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getAftersaleStatusList()
    {
        return ['-1' => __('拒绝'), '0' => __('未处理'), '1' => __('处理中'), '2' => __('售后完成')];
    }

    public function getRefundStatusList()
    {
        return ['-1' => __('Refund_status -1'), '0' => __('Refund_status 0'), '1' => __('Refund_status 1')];
    }
    public function getRefundStatusList1()
    {
        return ['-1' => __('拒绝退款'), '0' => __('退款中'), '1' => __('退款完成')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReturnTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['return_time']) ? $data['return_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAftersaleStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['aftersale_status']) ? $data['aftersale_status'] : '');
        $list = $this->getAftersaleStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList1();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getConfirmTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['confirm_time']) ? $data['confirm_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRefundTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAgreeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['agree_time']) ? $data['agree_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setReturnTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setConfirmTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRefundTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAgreeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function order()
    {
        return $this->belongsTo('app\common\model\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function item()
    {
        return $this->belongsTo('Item', 'order_item_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
